package com.example.yeujian5.yuejian5.model;

import com.example.yeujian5.yuejian5.Interface.UserFace;
import com.example.yeujian5.yuejian5.Listenner.PersonListener;
import com.example.yeujian5.yuejian5.Listenner.UserAllOrderListener;
import com.example.yeujian5.yuejian5.Listenner.UserCollectListener;
import com.example.yeujian5.yuejian5.Listenner.UserListener;
import com.example.yeujian5.yuejian5.Listenner.UserOrderInfoListener;
import com.example.yeujian5.yuejian5.Listenner.UserPhotoListener;
import com.example.yeujian5.yuejian5.bean.PictureBean;
import com.example.yeujian5.yuejian5.bean.UserBean;
import com.example.yeujian5.yuejian5.bean.UserCollect;
import com.example.yeujian5.yuejian5.bean.UserOrder;
import com.example.yeujian5.yuejian5.bean.UserOrderInfo;
import com.example.yeujian5.yuejian5.service.UserService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangmengmeng on 2017/9/28.
 */

public class UserModel implements UserFace {

    private UserService userService;
    private Retrofit retrofit;
    private static final String BASE_URL="http://172.24.10.175/workout/api.php/";//主机地址

    public UserModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//解析方式
                .build();
    }

    /**
     * 用户登录模块
     * @param tel 电话号码
     * @param password 密码
     * @param userListener
     */

    @Override
    public void getLogin(String tel, String password, final UserListener userListener) {
        userService=retrofit.create(UserService.class);
        Call<UserBean> call=userService.LoginImpl(tel,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful()&&response.body().getError()==null)
                    userListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                userListener.onFail("登录失败!");
            }
        });
    }

    /**
     * 用户注册模块
     * @param tel 电话号码
     * @param password 密码
     * @param userListener
     */
    @Override
    public void getRegister(String realname,String tel, String password, final UserListener userListener) {
        userService=retrofit.create(UserService.class);
        Call<UserBean> call=userService.RegImpl(realname,tel,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                String result = response.body().getError();
                if(!"手机号已被使用".equals(result))
                    userListener.onResponse(response.body());
                else{
                    userListener.onFail("手机号已存在");
                }
//                if (response.isSuccessful()&&response.body().getError()==null) {
//                    userListener.onResponse(response.body());
//                }
            }
            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                userListener.onFail("注册失败!");
            }
        });
    }
    @Override
    public void getUserByID(String SessionID, String userid,final PersonListener personListener) {
        userService = retrofit.create(UserService.class);
        Call<UserBean> call =userService.getUserById("lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful() && response.body() != null) {
                    personListener.onResponse(response.body());
                } else {
                    personListener.onFail("123");
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                personListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void EditUser(String SessionID, String userid, String password, final PersonListener personListener) {
        userService = retrofit.create(UserService.class);
        Call<UserBean> call =userService.EditUser("lgj98rfkiqvenhrk2o7oe2po81",userid,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful() && response.body() != null) {
                    personListener.onResponse(response.body());
                } else {
                    personListener.onFail("123");
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                personListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getcollect(String userid, final UserCollectListener usercollectListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserCollect>> call =userService.getcollect("fans","lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<List<UserCollect>>() {
            @Override
            public void onResponse(Call<List<UserCollect>> call, Response<List<UserCollect>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    usercollectListener.onResponse(response.body());
                } else {
                    usercollectListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserCollect>> call, Throwable t) {
                usercollectListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getShaituByUser(String userid, final UserPhotoListener userphotoListener) {
        userService = retrofit.create(UserService.class);
        Call<List<PictureBean>> call =userService.getShaituByUser("shaitu","lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<List<PictureBean>>() {
            @Override
            public void onResponse(Call<List<PictureBean>> call, Response<List<PictureBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    userphotoListener.onResponse(response.body());
                } else {
                    userphotoListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<PictureBean>> call, Throwable t) {
                userphotoListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getAllOrderByUser(String userid,final UserAllOrderListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserOrder>> call =userService.getAllOrderByUser("sign",userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<List<UserOrder>>() {
            @Override
            public void onResponse(Call<List<UserOrder>> call, Response<List<UserOrder>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserOrder>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getOrderByType(String userid,String vstate,final UserAllOrderListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserOrder>> call =userService.getOrderByType("sign",userid,vstate,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<List<UserOrder>>() {
            @Override
            public void onResponse(Call<List<UserOrder>> call, Response<List<UserOrder>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserOrder>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getSignByID(String id, String type, final UserOrderInfoListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<UserOrderInfo> call =userService.getSignById(id,type,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<UserOrderInfo>() {
            @Override
            public void onResponse(Call<UserOrderInfo> call, Response<UserOrderInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<UserOrderInfo> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void NoSign(String id, String type, final UserOrderInfoListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<UserOrderInfo> call =userService.NoSign(id,type,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<UserOrderInfo>() {
            @Override
            public void onResponse(Call<UserOrderInfo> call, Response<UserOrderInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<UserOrderInfo> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    public Call<Integer> Nocollect(String club_id, String user_id){
        UserService userService = retrofit.create(UserService.class);
        return  userService.Nocollect("clubfans",club_id,user_id,"lgj98rfkiqvenhrk2o7oe2po81");
    }


}
